<template>
  <div class="login-page">
      <div>
        <van-nav-bar
        title="账号登录"
        left-arrow
        @click-left="$router.back()"
      />
      </div>

    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[
          { required: true, message: '请填写用户名' },
          { pattern: /^\w{5,}/, message: '用户名至少为5位' },
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^\w{6,}/, message: '密码至少为6位数' },
        ]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
        <div class="register">
          <router-link class="link" to="/registerre"
            >还没有账号，去注册~</router-link
          >
        </div>
      </div>
    </van-form>
  </div>
</template>

<script>
// 按需导入{}
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (value) {
      // console.log(value)
      const { body } = await login(value)
      if (body.token) {
        this.$toast.success('登录成功')
        this.$router.push('/myroom')
      }
      setToken(body.token)
    }
  }
}
</script>

<style lang="less" scoped>
h2 {
  color: #fff;
  line-height: 60px;
}
.register {
  width: 160px;
  height: 60px;
  margin-top: 20px;
  margin-left: 90px;
}
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
